package me.shaohui.shareutil;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;

import com.colin.aiyi.R;
import com.colin.aiyi.app.MyApp;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXTextObject;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by hl on 2017/6/21.
 */

public class WXUtil {
private static final int THUMB_SIZE = 150;

public  static  void  sendReqToWX(String url,String title,String des,Bitmap bmp){
    if (url==null)
        return;
    if (title==null)
        title="";
    if (des==null)
        des="";

    WXWebpageObject webpage = new WXWebpageObject();
    webpage.webpageUrl =url;
    WXMediaMessage msg = new WXMediaMessage(webpage);
    msg.title = title;
    msg.description =des;
    if (bmp!=null){
        Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, THUMB_SIZE, THUMB_SIZE, true);
        bmp.recycle();
        msg.thumbData = Util.bmpToByteArray2(thumbBmp, true);
    }
    SendMessageToWX.Req req = new SendMessageToWX.Req();
    req.transaction = buildTransaction("webpage");
    req.message = msg;
    req.scene = SendMessageToWX.Req.WXSceneSession;
    MyApp.getInstance().getApi().sendReq(req);
}

    public  static  void  sendReqToWXQ(String url,String title,String des,Bitmap bmp){
        if (url==null)
            return;
        if (title==null)
            title="";
        if (des==null)
            des="";

        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl =url;
        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title = title;
        msg.description =des;
        if (bmp!=null){
            Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, THUMB_SIZE, THUMB_SIZE, true);
            bmp.recycle();
            msg.thumbData = Util.bmpToByteArray2(thumbBmp, true);
        }
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("webpage");
        req.message = msg;
        req.scene =SendMessageToWX.Req. WXSceneTimeline;
        MyApp.getInstance().getApi().sendReq(req);
    }


    public  static  void  sendReqToWXText(IWXAPI api){
        WXTextObject textObject=new WXTextObject();
        textObject.text="分享测试";
        WXMediaMessage msg=new WXMediaMessage();
        msg.mediaObject=textObject;
        msg.description="fenxiang";
        SendMessageToWX.Req req=new SendMessageToWX.Req();
        req.transaction=buildTransaction("text");
        req.message=msg;
        req.scene=SendMessageToWX.Req.WXSceneTimeline;
        api.sendReq(req);
    }

    private static String buildTransaction(final String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
    }

    /**
     * 根据一个网络连接(String)获取bitmap图像
     *
     * @param
     * @return
     * @throws
     */
    public static Bitmap getBitmap(String src) {
        try {
            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);

            //设置固定大小
            //需要的大小
            float newWidth = 200f;
            float newHeigth = 200f;

            //图片大小
            int width = myBitmap.getWidth();
            int height = myBitmap.getHeight();

            //缩放比例
            float scaleWidth = newWidth / width;
            float scaleHeigth = newHeigth / height;
            Matrix matrix = new Matrix();
            matrix.postScale(scaleWidth, scaleHeigth);
            Bitmap bitmap = Bitmap.createBitmap(myBitmap, 0, 0, width, height, matrix, true);
            return bitmap;
        } catch (IOException e) {
            // Log exception
            return null;
        }
    }


}
